//
//  GKBrowseDataQueue.swift
//  MySwiftObject
//
//  Created by wangws1990 on 2019/9/11.
//  Copyright © 2019 wangws1990. All rights reserved.
//

import UIKit
let tableBrowse = "bookBrowse";
let primaryBrowId = "bookId";
class GKBrowseDataQueue: NSObject {
    class func insertBookModel(browse:GKBrowseModel,completion: @escaping ((_ success : Bool) ->Void)){
        BaseDataQueue.insertData(toDataBase:tableBrowse, primaryId: primaryBrowId, userInfo:browse.toJSON()!, completion: completion)
    }
    class func deleteBookModel(bookId:String,completion:@escaping ((_ success : Bool) ->())){
        BaseDataQueue.deleteData(toDataBase: tableBrowse, primaryId: primaryBrowId, primaryValue: bookId, completion: completion);
    }
    class func getBookModel(bookId:String,completion:@escaping ((_ bookModel : GKBrowseModel) ->Void)){
        BaseDataQueue.getDataFromDataBase(tableBrowse, primaryId: primaryBrowId, primaryValue: bookId) { (object) in
            let model : GKBrowseModel = GKBrowseModel.deserialize(from: (object as NSDictionary))!
            completion(model);
        }
    }

    class func getBookModel(page:NSInteger,size:NSInteger,completion:@escaping ((_ listData : [GKBrowseModel]) ->Void)){
        BaseDataQueue.getDatasFromDataBase(tableBrowse, primaryId:primaryBrowId, page:page, pageSize: size) { (respond) in
            var dats:[GKBrowseModel] = [];
            let list : NSArray = respond as NSArray;
            for obj in list{
                var dic:Dictionary<String,Any> = obj as! Dictionary<String,Any>
                dic.removeValue(forKey: "chapterInfo")
                let model : GKBrowseModel = GKBrowseModel.deserialize(from: dic)!;
                dats.append(model);
            }
            completion(dats);
        };
    }
}
